var l = null;
angular
		.module("CoordinadorApp")
		.controller(
				"TestCtrl",
				function($scope) {
					$scope.estudiantes = [
					                      {nombre:"Perez",nota:"",id:0},
					                      {nombre:"Rueda",nota:"",id:1},
					                      {nombre:"Flórez",nota:"",id:2},
					                      {nombre:"Navas",nota:"",id:3},
					                      {nombre:"Giraldo",nota:"",id:4},
					                      {nombre:"Rodriguez",nota:"",id:5},
					                      {nombre:"Caceres",nota:"",id:6}
					                      ];
					$scope.mapaEstudiantes={};
					for(e in $scope.estudiantes)
						$scope.mapaEstudiantes[e] = $scope.estudiantes[e].nombre.toLowerCase();
					
					function match(s){
						console.log("Result:"+ s);
						text = s.split(" ");
						if(text.length != 2)
							return;
						for(e in $scope.mapaEstudiantes)
							if(	$scope.mapaEstudiantes[e] == text[0]){
								$scope.estudiantes[e].nota = text[1];
								
								return;
							}
					}
					
					$scope.langs = [
							[ 'Deutsch', [ [ 'de-DE', 'Deutschland' ] ] ],
							[
									'English',
									[ [ 'en-AU', 'Australia' ],
											[ 'en-CA', 'Canada' ],
											[ 'en-IN', 'India' ],
											[ 'en-NZ', 'New Zealand' ],
											[ 'en-ZA', 'South Africa' ],
											[ 'en-GB', 'United Kingdom' ],
											[ 'en-US', 'United States' ] ] ],
							[
									'Español',
									[
											[ 'es-AR', 'Argentina' ],
											[ 'es-CL', 'Chile' ],
											[ 'es-CO', 'Colombia' ],
											[ 'es-CR', 'Costa Rica' ],
											[ 'es-EC', 'Ecuador' ],
											[ 'es-SV', 'El Salvador' ],
											[ 'es-ES', 'España' ],
											[ 'es-US', 'Estados Unidos' ],
											[ 'es-GT', 'Guatemala' ],
											[ 'es-HN', 'Honduras' ],
											[ 'es-MX', 'México' ],
											[ 'es-NI', 'Nicaragua' ],
											[ 'es-PA', 'Panamá' ],
											[ 'es-PY', 'Paraguay' ],
											[ 'es-PE', 'Perú' ],
											[ 'es-PR', 'Puerto Rico' ],
											[ 'es-DO', 'República Dominicana' ],
											[ 'es-UY', 'Uruguay' ],
											[ 'es-VE', 'Venezuela' ] ] ],
							[ 'Français', [ [ 'fr-FR', 'Fance' ] ] ],
							[
									'Italiano',
									[ [ 'it-IT', 'Italia' ],
											[ 'it-CH', 'Svizzera' ] ] ],
							[
									'Português',
									[ [ 'pt-BR', 'Brasil' ],
											[ 'pt-PT', 'Portugal' ] ] ] ];

					$scope.startButton = function(event) {
						if (recognizing) {
							recognition.stop();
							return;
						}
						final_transcript = '';
						recognition.lang = $scope.selectedDialect;
						recognition.start();
						ignore_onend = false;
						final_span.innerHTML = '';
						interim_span.innerHTML = '';
						showInfo('info_allow');
						start_timestamp = event.timeStamp;
					};
					$scope.updateCountries = function() {
						console.log("lan:");
						console.log($scope.selectedLanguage);
						l = $scope.selectedLanguage;
						// $scope.selectedDialect=$scope.selectedLanguage[1][0][0];
						$scope.dialects = $scope.selectedLanguage[1];
						console.log("dia1:");
						console.log($scope.selectedLanguage[1]);
						console.log("dia2:");
						console.log($scope.dialects);
						$scope.selectedDialect = "es-CO";
					}
					// $scope.selectedLanguage=$scope.langs[2];
					// $scope.updateCountries();
					$scope.selectedDialect = "es-CO";
					var final_transcript = '';
					var recognizing = false;
					var ignore_onend;
					var start_timestamp;
					if (!('webkitSpeechRecognition' in window)) {
						console.log("upgrade!!!!!");
						return;
					}

					start_button.style.display = 'inline-block';
					var recognition = new webkitSpeechRecognition();
					$scope.recognition =recognition ;
					recognition.continuous = true;
					recognition.interimResults = true;

					recognition.onstart = function() {
						recognizing = true;
						start_button.innerHTML = "Escuchando";
					};

					recognition.onerror = function(event) {
						if (event.error == 'no-speech') {
							showInfo('info_no_speech');
							ignore_onend = true;
						}
						if (event.error == 'audio-capture') {
							showInfo('info_no_microphone');
							ignore_onend = true;
						}
						if (event.error == 'not-allowed') {
							if (event.timeStamp - start_timestamp < 100) {
								showInfo('info_blocked');
							} else {
								showInfo('info_denied');
							}
							ignore_onend = true;
						}
					};

					recognition.onend = function() {
						recognizing = false;
						if (ignore_onend) {
							return;
						}
						start_button.innerHTML = 'Start!!';
						if (!final_transcript) {
							return;
						}
						if (window.getSelection) {
							window.getSelection().removeAllRanges();
							var range = document.createRange();
							range.selectNode(document
									.getElementById('final_span'));
							window.getSelection().addRange(range);
						}
					};

					recognition.onresult = function(event) {
						var interim_transcript = '';
						if (typeof (event.results) == 'undefined') {
							recognition.onend = null;
							recognition.stop();
							upgrade();
							return;
						}
						for ( var i = event.resultIndex; i < event.results.length; ++i) {
							console.log("text: "+event.results[i][0].transcript+", confidence: "+event.results[i][0].confidence+ ", final: "+event.results[i].isFinal);
							if (event.results[i].isFinal) {
								final_transcript += event.results[i][0].transcript;
								match(event.results[i][0].transcript);
							} else {
								interim_transcript += event.results[i][0].transcript;
								if(event.results[i][0].confidence> 0.5)
									match(event.results[i][0].transcript);
							}
						}
						final_transcript = capitalize(final_transcript);
						final_span.innerHTML = linebreak(final_transcript);
						interim_span.innerHTML = linebreak(interim_transcript);
					};

					var two_line = /\n\n/g;
					var one_line = /\n/g;
					function linebreak(s) {
						return s.replace(two_line, '<p></p>').replace(one_line,
								'<br>');
					}

					var first_char = /\S/;
					function capitalize(s) {
						return s.replace(first_char, function(m) {
							return m.toUpperCase();
						});
					}							

					function showInfo(str) {
						console.log(str);
					}									
				});
// angular.module("ProfesorApp").factory("Voice",function($scope){});
